package com.cqs.leetcode.queueStack;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author lixiaowen
 * @create 2019-12-13
 */
public class ImplementStackusingQueues225 {

    private Queue<Integer> queue  = new LinkedList<>();
    private Queue<Integer> helper = new LinkedList<>();


    public void push(int x) {
        //模拟逆序入队列
        helper.offer(x);
        helper.addAll(queue);
        queue.clear();
        //交换
        Queue<Integer> tmp = helper;
        helper = queue;
        queue = tmp;
        //O(N)
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        return queue.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }

    public static void main(String[] args) {
        ImplementStackusingQueues225 stack = new ImplementStackusingQueues225();
        stack.push(3);
        stack.push(2);
        System.out.println(stack.top());
        System.out.println(stack.pop());
        System.out.println(stack.empty());
        stack.push(1);
        System.out.println(stack.pop());
        System.out.println(stack.top());
        System.out.println(stack.pop());
        System.out.println(stack.empty());

    }
}
